package com.habitsoft.glassfish.dashboard.client;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.habitsoft.kiyaa.caching.CacheMap;
import com.habitsoft.kiyaa.caching.CachedQuery;
import com.habitsoft.kiyaa.caching.ChildrenByOffsetLimitKey;

public class LogServiceCachingProxy implements LogServiceAsync {
    final LogServiceAsync service;
    CacheMap<String, Integer> counts = new CacheMap<String, Integer>(CachedQuery.VERY_SHORT_EXPIRY);
    CacheMap<ChildrenByOffsetLimitKey<String>, String[]> logMessages = new CacheMap<ChildrenByOffsetLimitKey<String>, String[]>(CachedQuery.VERY_SHORT_EXPIRY);
    CachedQuery<String[]> logFiles = new CachedQuery<String[]>(CachedQuery.VERY_SHORT_EXPIRY);
    
    public LogServiceCachingProxy(LogServiceAsync service) {
        this.service = service;
    }

    public void countLogMessages(String logFile, String filter, AsyncCallback<Integer> callback) {
        if((callback = counts.fetch(logFile+":"+filter, callback)) != null)
            service.countLogMessages(logFile, filter, callback);
    }

    public void getLogMessages(String logFile, String filter, int offset, int limit, AsyncCallback<String[]> callback) {
        if((callback = logMessages.fetch(new ChildrenByOffsetLimitKey<String>(logFile+":"+filter, offset, limit), callback)) != null) {
            service.getLogMessages(logFile, filter, offset, limit, callback);
        }
    }

    public void listLogFiles(AsyncCallback<String[]> callback) {
        if((callback = logFiles.fetch(callback)) != null)
            service.listLogFiles(callback);
    }

}
